//
//  StudyViewController.m
//  NoNameAmazing
//
//  Created by hero on 2017/9/29.
//  Copyright © 2017年 北京执行力科技有限公司. All rights reserved.
//

#import "StudyViewController.h"
#import "CommenWebViewController.h"
#import "BaseTabBarModel.h"
#import "SDCycleScrollView.h"

@interface StudyViewController ()
@property (nonatomic ,strong) NSMutableArray *dataSource;
@property (nonatomic ,strong) NSMutableArray *titleArr;
@property (nonatomic ,strong) NSMutableArray *imgArr;
@property (nonatomic ,strong) UIImageView * imageview;
@property(nonatomic,strong)NSMutableArray * urlArr;
@property(nonatomic,strong)NSMutableArray * bannerArr;
@property(nonatomic,strong)SDCycleScrollView *sycleScrollView;
@end

@implementation StudyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestLB];
    
    
}
-(void)requestLB
{
    NSDictionary *params = @{@"name":@"carouselshow"};
    [NetWorkManager requestWithType:1 withUrlString:@"/plugin/getPluginByName.do" withParaments:params withSuccessBlock:^(id object) {
        NSString *imageName;
        if ([object[@"status"] integerValue] == 1) {
            NSString *url = object[@"pluginName"];
            imageName = url;
            NSString *urlStr = [NSString stringWithFormat:@"/%@/webService/carouselService/carousel/getCarousel/studyBanner/%@",url,[us objectForKey:kRoleid]];
            [NetWorkManager requestWithType:0 withUrlString:urlStr withParaments:@"" withSuccessBlock:^(id object) {
                if ([object[@"status"]integerValue] == 1) {
                    [self loadData];
                    _bannerArr = [NSMutableArray array];
                    if ([object[@"status"]integerValue] == 1) {
                        
                        for (NSString *str  in object[@"carousel"]) {
                            NSString *BaseURL = [us objectForKey:baseUrl];
                            NSString *str1 = [NSString stringWithFormat:@"%@/%@/%@",BaseURL,imageName,str];
                            ;
                            [_bannerArr addObject:str1];
                        }
                        
                    }
                    
                }
            } withFailureBlock:^(NSError *error) {
                
            } progress:^(float progress) {
                
            }];
        }
    } withFailureBlock:^(NSError *error) {
        
    } progress:^(float progress) {
        
    }];
    
}


- (void)createUI{
    
    _sycleScrollView = [[SDCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 125)];
    _sycleScrollView.currentPageDotImage = [UIImage imageNamed:@"point_black-1"];
    _sycleScrollView.pageDotImage = [UIImage imageNamed:@"point_black_pre-1"];
    _sycleScrollView.autoScroll = YES;
    _sycleScrollView.currentPageDotColor = [UIColor whiteColor];
    _sycleScrollView.imageURLStringsGroup = _bannerArr;
    [self.view addSubview:_sycleScrollView];
    
    self.view.backgroundColor= UIColorFromRGB(0xf6f6f6);
    UIScrollView * scro = [[UIScrollView alloc]init];
    [self.view addSubview:scro];
    scro.frame = CGRectMake(0, 125, kScreen_Width, kScreen_Height-125-49);
    if(self.dataSource.count%3==0){
        scro.contentSize = CGSizeMake(0,(kScreen_Width-2)/3*(self.dataSource.count/3+1)+10);
    }else{
        scro.contentSize = CGSizeMake(0,(kScreen_Width-2)/3*(self.dataSource.count/3+2)+10);
    }
    CGFloat space = 1;
    CGFloat btnWidth = (kScreen_Width-2)/3;
    CGFloat btnHeight =(kScreen_Width-2)/3;
    for (int i= 0; i<self.dataSource.count; i++) {
        UIButton *button = [[UIButton alloc]init];
        button.backgroundColor = [UIColor whiteColor];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = i+1;
        button.frame = CGRectMake(btnWidth*(i%3)+(i%3)*space,btnHeight*(i/3)+(i/3)*space , btnWidth, btnHeight);
        [scro addSubview:button];
        NSString *BaseURL = [us objectForKey:baseUrl];
        UIImageView *img = [[UIImageView alloc]init];
        [img sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BaseURL,self.imgArr[i]]] placeholderImage:[UIImage imageNamed:@"申请"]];
        img.frame = CGRectMake(btnWidth/2-20, btnHeight/2-30, 40, 40);
        UILabel *label = [[UILabel alloc]init];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = self.titleArr[i];
        [label setFrame:CGRectMake(0, CGRectGetMaxY(img.frame)+10,btnWidth, 20)];
        [button addSubview:label];
        [button addSubview:img];
    }
}
- (void)buttonClick:(UIButton *)btn{
    CommenWebViewController *web = [[CommenWebViewController alloc]init];
    if (![self.urlArr[btn.tag-1] isEqualToString:@" "] &&self.urlArr[btn.tag-1]!=nil) {
        web.urlStr =self.urlArr[btn.tag-1];
        [self.navigationController pushViewController:web animated:YES];
    }
    
}
- (void)loadData{
    NSDictionary *params = @{@"roleid":[us objectForKey:kRoleid],@"pid":[us objectForKey:kStudentsMenuId2]};
    [NetWorkManager requestWithType:1 withUrlString:@"/appmenu/appgetMenubyPid.do" withParaments:params withSuccessBlock:^(id object) {
        if ([object[@"status"] integerValue] == 1) {
            for (NSDictionary *dict in object[@"menuList"][1][@"secondList"]) {
                BaseTabBarModel *model = [BaseTabBarModel modelWithDict:dict];
                [self.titleArr addObject:model.MENUNAME];
                [self.imgArr addObject:model.MENUIMG];
                [self.urlArr addObject:model.MENURESOURCEURL];
                [self.dataSource addObject:model];
            }
            
            [self createUI];
        }
        
    } withFailureBlock:^(NSError *error) {
        
    } progress:^(float progress) {
        
    }];
}
#pragma mark - lazyinit -
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (NSMutableArray *)titleArr{
    if (!_titleArr) {
        _titleArr = [NSMutableArray array];
    }
    return _titleArr;
}
- (NSMutableArray *)imgArr{
    if (!_imgArr) {
        _imgArr = [NSMutableArray array];
    }
    return _imgArr;
}
- (NSMutableArray *)urlArr{
    if (!_urlArr) {
        _urlArr = [NSMutableArray array];
    }
    return _urlArr;
}
- (NSMutableArray *)bannerArr{
    if (!_bannerArr) {
        _bannerArr = [NSMutableArray array];
    }
    return _bannerArr;
}


@end
